//
//  YYAnimatedImageView+Extension.m
//  PayBox
//
//  Created by Hayder on 2022/2/25.
//   解决iOS14下YYImage黑屏问题

#import "YYAnimatedImageView+Extension.h"
#import <YYImage/YYImage.h>
#import <YYWebImage/YYWebImage.h>
#import <objc/runtime.h>
#import "HHGlobalDefine.h"

@implementation YYAnimatedImageView (Extension)

+(void)load
{
    // hook：钩子函数
    Method method1 = class_getInstanceMethod(self, @selector(displayLayer:));
    
    Method method2 = class_getInstanceMethod(self, @selector(dx_displayLayer:));
    method_exchangeImplementations(method1, method2);
}

-(void)dx_displayLayer:(CALayer *)layer {
    
    if ([UIImageView instancesRespondToSelector:@selector(displayLayer:)]) {
        [super displayLayer:layer];
    }
    
}


- (void)setWebImage:(NSString *)urlString
{
    if(!urlString)
    {
        urlString = @"";
    }
    UIImage *placeHolder = [UIImage imageWithColor:kGray_240];
    NSString *iconUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self yy_setImageWithURL:[NSURL URLWithString:iconUrl] placeholder:placeHolder];

}
- (void)setWebImage:(NSString *)urlString placeHolder:(UIImage *)image{
    if(!urlString)
    {
        urlString = @"";
    }
    NSString *iconUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self yy_setImageWithURL:[NSURL URLWithString:iconUrl] placeholder:image];
}

- (void)setWebImage:(NSString *)urlString placeHolder:(UIImage *)placeHolder Completion:(void (^)(UIImage *image))Completion
{
    NSString *iconUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self yy_setImageWithURL:[NSURL URLWithString:iconUrl] placeholder:placeHolder options:YYWebImageOptionIgnoreAnimatedImage completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if(Completion)
        {
            Completion(image);
        }
    }];
}
@end

